package com.leiyuee.flink.batch.util;

import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;
import java.util.stream.Stream;

/**
 * @author Yu_Lei
 * @date 2024/6/27
 */
public class FileUtil {
    //1.获取小于1M的文件
    public static List<File> getFilesLessThan1MB(String directoryPath) {
        File directory = new File(directoryPath);
        List<File> filesLessThan1MB = new ArrayList<>();

        if (directory.isDirectory()) {
            File[] files = directory.listFiles();
            if (files != null) {
                for (File file : files) {
                    if (file.isFile() && file.length() < 1024 * 1024) {
                        filesLessThan1MB.add(file);
                    }
                }
            }
        }

        return filesLessThan1MB;
    }

    //2.获取指定日期的文件

    public static List<File> getFilesByDate(String directoryPath) {
//         directoryPath = "SQLscripts/2024-06-27";
        Path directory = Paths.get(directoryPath);
        List<File> list = new ArrayList<>();
        try (Stream<Path> stream = Files.walk(directory)) {
            stream.filter(Files::isRegularFile)
                    .forEach(file -> list.add(file.toFile()));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return list;
    }

    //3.获取指定文件夹最近N个小时的文件
    public static List<File> getRecentNHoursFiles(String directoryPath, int n) {
        File directory = new File(directoryPath);
        List<File> files = new ArrayList<>();

        if (directory.isDirectory()) {
            File[] subFiles = directory.listFiles();
            if (subFiles != null) {
                for (File file : subFiles) {
                    if (file.isFile() && file.lastModified() > System.currentTimeMillis() - n * 60 * 60 * 1000) {
                        files.add(file);
                    }
                }
            }
        }

        return files;
    }

    //获取已经写入的文件名称
    private static Set<String> getLastDateSet(String path) {
        File file = new File(path);
        HashSet<String> result = new HashSet<>();
        if (!file.exists()) {
            return result; // 文件不存在，返回空字符串
        }
        try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
            String line;
            String lastLine = "";
            while ((line = reader.readLine()) != null) {
                lastLine = line;
            }
            String[] split = lastLine.split(",");
            return new HashSet<>(Arrays.asList(split));
        } catch (Exception ex) {
            return result;
        }
    }
    //4.然后将数据写到文件中
    public static void writeToFile(Set<String> dataSet, String filePath) {
        try {
            File file = new File(filePath);
            if (!file.exists()) {
                file.createNewFile();
            }
            FileWriter fw = new FileWriter(file.getAbsoluteFile());
            BufferedWriter bw = new BufferedWriter(fw);
            bw.write(String.join(",", dataSet));
            bw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
